package com.bdqn.maven.entity;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.*;

@Entity
@Table(name = "user")
public class User {
    private Integer id;
    private String password;
    private String username;
    private String phone;
    private String role;
    private Integer sms;
    private Double traffic;
    private Double bill;
    private Double balance;
    private Integer userpackage;
    private Integer extra_sms;
    private Integer extra_traffic;
    private Integer extra_bill;
    

    // 移除 @ElementCollection 相关注解
    private List<String> logs = new ArrayList<>();

    public User() {
        this.logs = new ArrayList<>();
    }

    public Integer getId() {
		return id;
	}



	public void setId(Integer id) {
		this.id = id;
	}



	public String getPassword() {
		return password;
	}



	public void setPassword(String password) {
		this.password = password;
	}



	public String getUsername() {
		return username;
	}



	public void setUsername(String username) {
		this.username = username;
	}



	public String getPhone() {
		return phone;
	}



	public void setPhone(String phone) {
		this.phone = phone;
	}



	public String getRole() {
		return role;
	}



	public void setRole(String role) {
		this.role = role;
	}



	public Integer getSms() {
		return sms;
	}



	public void setSms(Integer sms) {
		this.sms = sms;
	}



	public Double getTraffic() {
		return traffic;
	}



	public void setTraffic(Double traffic) {
		this.traffic = traffic;
	}



	public Double getBill() {
		return bill;
	}



	public void setBill(Double bill) {
		this.bill = bill;
	}



	public Double getBalance() {
		return balance;
	}



	public void setBalance(Double balance) {
		this.balance = balance;
	}



	public Integer getUserpackage() {
		return userpackage;
	}



	public void setUserpackage(Integer userpackage) {
		this.userpackage = userpackage;
	}



	public Integer getExtra_traffic() {
		return extra_traffic;
	}



	public void setExtra_traffic(Integer extra_traffic) {
		this.extra_traffic = extra_traffic;
	}



	public Integer getExtra_sms() {
		return extra_sms;
	}



	public void setExtra_sms(Integer extra_sms) {
		this.extra_sms = extra_sms;
	}



	public Integer getExtra_bill() {
		return extra_bill;
	}



	public void setExtra_bill(Integer extra_bill) {
		this.extra_bill = extra_bill;
	}



	public void setLogs(List<String> logs) {
		this.logs = logs;
	}

    // 清空日志列表的方法
    public void clearLogs() {
        this.logs.clear();
    }
 // 添加日志的方法
	public void addLog(String log) {
        if (log == null || log.trim().isEmpty()) {
            System.err.println("警告：尝试添加空日志内容");
            return;
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestamp = sdf.format(new Date());
        logs.add(timestamp + " - " + log);
    }

	// 获取日志列表的方法
    public List<String> getLogs() {
        return logs;
    }
    
    public void saveLogToFile(String filePath) {
        System.out.println("开始保存日志到文件: " + filePath);
        System.out.println("当前日志数量: " + logs.size());

        if (logs.isEmpty()) {
            System.out.println("警告：日志列表为空！");
            return;
        }

        try (FileWriter writer = new FileWriter(filePath)) {
            for (String log : logs) {
                writer.write(log + "\n");
            }
            System.out.println("日志保存成功！");
        } catch (IOException e) {
            System.err.println("保存日志时出错: " + e.getMessage());
            e.printStackTrace();
        }
    }

   

    @Override
	public String toString() {
		return "User [id=" + id + ", password=" + password + ", username=" + username + ", phone=" + phone + ", role="
				+ role + ", sms=" + sms + ", traffic=" + traffic + ", bill=" + bill + ", balance=" + balance
				+ ", userpackage=" + userpackage + ", extra_sms=" + extra_sms + ", extra_traffic=" + extra_traffic
				+ ", extra_balance=" + extra_bill + ", logs=" + logs + "]";
	}



	public double getPackagePrice(int packageId) {
        switch (packageId) {
            case 1:
                return 50.0;
            case 2:
                return 60.0;
            case 3:
                return 150.0;
            default:
                return 0.0;
        }
    }
}